package com.example.fanplant.controller;

import com.example.fanplant.model.Constant;
import com.example.fanplant.model.User;
import com.example.fanplant.model.Vitae_table;
import com.example.fanplant.respository.UserRepository;
import com.example.fanplant.respository.VitaeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

@RestController
@RequestMapping("/vitae")
public class VitaeController {

    @Autowired
    private VitaeRepository vitaeRepository;


    //查词所有用户  http://localhost:8080/vitae/vitaelist2?page=1
    @GetMapping("/vitaelist2")
    public ModelAndView vitaelist2(HttpServletRequest request, Model model) {  //success
        String pagestr = request.getParameter("page");
        System.err.println("vitaelist2====>>start:" + pagestr);
        if (pagestr == null || pagestr.isEmpty()) {
            pagestr = "1";
        }
        int page_ = Integer.parseInt(pagestr);
        System.err.println("vitaelist2====>>end:" + page_);

        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page_ - 1, Constant.PAGE_SIZE, sort);//success

        ;//success    分页查询 注意参数的类型，其中size要大于等于1，page则是从0开始算起
        Page<Vitae_table> userPage = vitaeRepository.findAll(pageable);
        model.addAttribute("vitaelist", userPage);

//        pageable.getPageNumber()
//        getPageSize 是查询到一页的数量
//        getPageNumber 是下标当前第几页
//        getOffset  查询的第一个数位于表的下标位置
        int allpage = userPage.getTotalPages();

        model.addAttribute("title", "用户管理");
        model.addAttribute("page1", true);
        model.addAttribute("page2", allpage >= 2 ? true : false);
        model.addAttribute("page3", allpage >= 3 ? true : false);
        model.addAttribute("page4", allpage >= 4 ? true : false);
        model.addAttribute("pagemore", allpage >= 5 ? true : false);

        model.addAttribute("currentPage", page_);
        model.addAttribute("pages", allpage);
        System.err.println("newsList====>>    page:" + page_ + "        PageSize " + allpage);

        return new ModelAndView("bootstarp/vitae-list", "vitae", model);
//        return new ModelAndView("user/userlist", "userModel", model);
    }


    //修改用户界面
    @GetMapping(value = "vitae_edit2/{id}")
    public ModelAndView editForm2(@PathVariable("id") Long id, Model model) {
        System.err.println("vitae_edit2====>>" + id);
        Optional<Vitae_table> vitae = vitaeRepository.findById(id);
        model.addAttribute("Vitae", vitae.get());
        model.addAttribute("title", "编辑用户");

        System.err.println("vitae_edit2====>>result:" + vitae.get().getName());
        return new ModelAndView("bootstarp/vitae-edit", "vitaeModel", model);
    }

    @PostMapping("{vitae_save_update}")
    public ModelAndView saveOrUpdateUser2(Vitae_table vitae_table) {
        System.err.println("saveOrUpdateUser2====>>" + vitae_table.getName() + "    " + vitae_table.getId());
        vitae_table = vitaeRepository.save(vitae_table);
//        if(user=user){//判断是否成功
//            success
//        }
        return new ModelAndView("redirect:/vitae/vitaelist2");
    }

    //根据id删除用户
    @GetMapping(value = "vitae_delete2/{id}")
    public ModelAndView delete2(@PathVariable("id") Long id) {
        System.err.println("vitae_delete2====>>" + id);   //fail  不存在会报错
        vitaeRepository.deleteById(id);

        return new ModelAndView("redirect:/vitae/vitaelist2");
    }


    //获取创建表单页面
    @GetMapping("/vitae_form2")
    public ModelAndView createForm2(Model model) {
        System.err.println("vitae_form2====>>" + model.toString());
        model.addAttribute("Vitae", new Vitae_table(null, null, null));
        model.addAttribute("title", "创建用户");
        return new ModelAndView("bootstarp/vitae-add", "vitaeModel", model);
    }



    //根据id 查询用户
    @GetMapping("vitae_info/{id}")
    public ModelAndView view(@PathVariable("id") Long id, Model model) {
        System.err.println("vitae_info====>>" + id);
        Optional<Vitae_table> vitae = vitaeRepository.findById(id);
        System.err.println("vitae_info====>>" + vitae.get().getName()+"     :"+vitae.get().getRegion().getRegion_name());
        model.addAttribute("vitae", vitae.get());
        model.addAttribute("title", "查看用户");
        return new ModelAndView("bootstarp/vitae-info", "vitaeModel", model);
    }


}